/*
 * @Author: duanguang
 * @Date: 2021-11-24 23:20:35
 * @LastEditTime: 2022-04-27 19:11:08
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/service/branch.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
/*
 * @Author: linzeqin
 * @Date: 2019-09-30 09:07:07
 * @description: 沙箱资源调度service
 */
import { Service } from 'egg';
import { PageModel } from '@/model/responseModel';
import { auditStatusEnum, UserRoleEnum } from '@/constants/enum.types';
import { BranchApply,IBranchApplyModel } from '@/model/branch_apply';
export default class BranchService extends Service {
    

    /**
     * 新增
     * @memberof RepositoryProjectService
     */
    public async createBranchApply(params: Partial<BranchApply>) {
        /** 新增 */
        try {
            return await this.ctx.model.BranchApply.create({
                ...params,
            } as BranchApply);
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    /** 根据id查询删除项目 */
    public async delete(id: string,userRule: Number,
        useruid:string): Promise<boolean> {
        try {
            const deleteResult = await this.ctx.model.BranchApply.deleteOne({
                _id: id,
                ...userRule > UserRoleEnum.admin&&{creator_id:useruid}
            });
            if (deleteResult.deletedCount) {
                return true
            }
            return false
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    public async reject(id: string,userRule: Number,userid: string) {
        try {
            const result = await this.ctx.model.BranchApply.updateOne({
                _id: id,
                ...userRule > UserRoleEnum.admin&&{assign_members_id:userid}
            },{
                audit_status:auditStatusEnum.reject
            });
            if (result.nModified) {
                return true
            }
            return false
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    public async branchApplyPass(params: Partial<BranchApply> & {
        id: string,
        userRule: Number,
        userId:string
    }) {
        return this.editBranchApply(params)
    }
    public async editBranchApply(params: Partial<BranchApply> & {
        id: string,
        userRule: Number,
        userId:string
    }) {
        if (params.id) {
            try {
                const {userId} =params
                const res = await this.ctx.model.BranchApply.updateOne({
                    _id: params.id,
                    $or: [ { audit_status: auditStatusEnum.reject }, { audit_status: auditStatusEnum.waiting }],
                    ...{assign_members_id:userId}
                },{
                    audit_status: params.audit_status
                });
                if (res.nModified) {
                    return await this.ctx.model.BranchApply.findById(params.id);
                }
            } catch (error) {
                
            }
        }
    }
    public async getList(params: {
        /** 当前页 */
        pageIndex: string,
        /** 每页条数 */
        pageSize: string,
        /** 分支名称 */
        branch_name: string,
        audit_status:string,
        userRule: Number,
        userId:string
    }) {
        try {
            const branch_name = params.branch_name || ''
            const audit_status = params.audit_status||''
            const pageIndex = parseInt(params.pageIndex,10)
            const pageSize = parseInt(params.pageSize,10)
            const condition = {
                $match: {
                    ...branch_name.trim() && { branch_name: { $regex: branch_name } },
                    ...audit_status.trim() && { audit_status: audit_status },
                    $or:[{creator_id:params.userId},{assign_members_id:params.userId}]
                },
            }
            const list = await this.ctx.model.BranchApply.aggregate([
                {
                    ...condition
                },
                {
                    $sort: {
                        update_time: -1,
                    },
                },
                {
                    $skip: (pageIndex - 1) * pageSize,
                },
                {
                    $limit: pageSize,
                },
            ])
            const total: number = await this.ctx.model.BranchApply.find(condition.$match).count();
            return new PageModel<IBranchApplyModel>({
                list,
                pageIndex: pageIndex || 1,
                pageSize: pageSize || 10,
                total,
            });
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
}
